package com.hongyun.tms.common.dto.pordwork;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.google.common.collect.Lists;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.format.annotation.DateTimeFormat;

import javax.validation.constraints.*;
import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 拖轮作业记录 新增参数
 *
 * @author liPeng
 * @date 2024-05-31
 **/
@Data
@Builder(toBuilder = true)
@NoArgsConstructor
@AllArgsConstructor
public class PordWorkSaveReqDto implements Serializable {

    @ApiModelProperty(value = "主键")
    private Long id;

    @ApiModelProperty(value = "计划ID")
    private Long planId;

    @ApiModelProperty(value = "拖轮ID")
    @NotNull(message = "拖轮ID不能为空")
    private Long tugId;

    @ApiModelProperty(value = "拖轮编号")
    @NotBlank(message = "拖轮编号不能为空")
    @Size(max = 30, message = "拖轮编号不能超过30个字符")
    private String tugNo;

    @ApiModelProperty(value = "开始作业时间")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private String beginTime;

    @ApiModelProperty(value = "结束作业时间")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private String endTime;

    @ApiModelProperty(value = "作业项目ID")
    @NotNull(message = "作业项目不能为空")
    private Long workItemId;

    @ApiModelProperty(value = "作业项目")
    @NotBlank(message = "作业项目不能为空")
    private String workItemName;

    @ApiModelProperty(value = "作业时长")
    private BigDecimal workHours;

    @ApiModelProperty(value = "客户ID")
    private Long customerId;

    @ApiModelProperty(value = "客户名称")
    private String customerName;

    @ApiModelProperty(value = "船舶ID")
    private Long shipId;

    @ApiModelProperty(value = "船名")
    private String shipName;

    @ApiModelProperty(value = "贸别：内贸，外贸")
    private String tradeType;

    @ApiModelProperty(value = "船舶类型代码")
    private String shipKindCode;

    @ApiModelProperty(value = "船舶类型名称")
    private String shipKindName;

    @ApiModelProperty(value = "拖轮费用")
    private BigDecimal amount;

    @ApiModelProperty(value = "备注")
    @Size(max = 150, message = "备注不能超过150个字符")
    private String remark;

    @ApiModelProperty(value = "0未审核1已审核2已计费")
    @NotNull(message = "请选择审核状态")
    private Integer status;

    @ApiModelProperty(value = "作业港区代码,字典")
    private String portAreaCode;

    @ApiModelProperty(value = "作业港区名称")
    private String portAreaName;

    @ApiModelProperty(value = "作业地点")
    @Size(max = 220, message = "作业地点不能超过220个字符")
    private String workLocation;

    @ApiModelProperty(value = "油耗")
    @Digits(integer = 10, fraction = 2)
    @DecimalMin(value = "0.00", message = "油耗不能小于0.00")
    @DecimalMax(value = "9999999.99", message = "油耗不能大于9999999.99")
    private BigDecimal oilConsume;


    @ApiModelProperty(value = "行驶里程")
    @Digits(integer = 10, fraction = 2)
    @DecimalMin(value = "0.00", message = "里程不能小于0.00")
    @DecimalMax(value = "9999999.99", message = "里程不能大于9999999.99")
    private BigDecimal distance;

    @ApiModelProperty(value = "作业记录和特殊记录，完工时上传附件")
    private List<String> attachList = Lists.newArrayList();

    @AssertTrue(message = "开始作业时间不能晚于结束作业时间")
    public boolean isBeginTimeBeforeEndTime() {
        if (StringUtils.isBlank(beginTime) || StringUtils.isBlank(endTime)) {
            return true; // 如果其中一个为空，不进行校验
        }

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime begin = LocalDateTime.parse(beginTime, formatter);
        LocalDateTime end = LocalDateTime.parse(endTime, formatter);

        return begin.isBefore(end);
    }
}

